package algutil.internet;

import algutil.internet.exception.AttributeNotFoundException;
import algutil.internet.exception.NoValueException;

public class Tag {

	private String source;

	public Tag(String source) {
		this.source = source;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getValues() throws NoValueException {
		String value = null;

		try {
			value = source.substring(source.indexOf(">") + 1,
					source.lastIndexOf("<")).trim();
		} catch (Exception e) {
			throw new NoValueException("Pas de valeur trouvee.");
		}

		return value;

	}

	public String getAttribute(String attributeName)
			throws AttributeNotFoundException {
		String attrib = null;

		try {
			attrib = source.substring(
					source.indexOf("\"", source.indexOf(attributeName)) + 1,
					source.indexOf("\"", source.indexOf("\"", source
							.indexOf(attributeName)) + 1)).trim();
		} catch (Exception e) {
			throw new AttributeNotFoundException("L'attribut '" + attributeName
					+ "' non trouve.");
		}

		return attrib;
	}
}
